<?php
ob_start();
include_once 'Core/Config.php';
include_once 'Model/ModelFather.php';
include_once 'Model/Usuario.php';
date_default_timezone_set('America/Campo_Grande');


if ($_SERVER['REQUEST_METHOD'] === 'POST') {

    $usuario = new Usuario(host, dbname, port, user, pass);
    $user = $usuario->find(array('order' => array('login'), 'condition' => array('login' => "'" . $_POST['loginPub'] . "'")));
    if (sizeof($user) == 1) {
        $salt = utf8_decode($user[0]['Usuario']['salt']);
        $senha = $salt . $_POST['senhaPub'];
        $senha = sha1($senha);
        if ($senha == $user[0]['Usuario']['senha']) {
            session_start();
            $_SESSION['user'] = $user;
            header("Location: index.php");
        } else {
            $result = 0;
        }
    }
}
?>
<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link rel="stylesheet" type="text/css" href="index.css" />
        <!-- jquery for equal height elements -->
        <script type="text/javascript" src="js/jquery-1.6.4.min.js"></script>
        <script type="text/javascript" src="js/formee.js"></script>
        <script type="text/javascript" src="js/jquery.click-calendario-1.0.js"></script>
        <script src="js/jQuery_vForms.js" type="text/javascript"></script>

        <!-- css for structure -->
        <link rel="stylesheet" href="css/formee-structure.css" type="text/css" media="screen" />

        <!-- css for style -->
        <link rel="stylesheet" href="css/formee-style.css" type="text/css" media="screen" />

        <link href="css/jquery.click-calendario-1.0.css" rel="stylesheet" type="text/css"/>
        <title>Biblioteca LATEC</title>
    </head>

    <body>

        <div id="wrap">
            <div id="header">
                <div id="logo">
                    <h1>Biblioteca LATEC</h1>
                </div>
                <div id="menu">
                    <li><a a href="front.php">Página Inicial</a></li>
                </div>
            </div>
            <div class="clear"></div>
            <div id="content">
                <?php
                if (isset($result) && $result == 1):
                    ?>
                    <?php
                elseif (isset($result) && $result == 0):
                    ?>
                    <div class="formee-msg-error">Login ou Senha incorretos</div>
                    <?php
                endif;
                ?>

                <form class="vForms formee" method="POST" action="login.php">
                    <fieldset>
                        <legend>Login</legend>
                        <div class="grid-6-12">
                            <label for="loginPub" class="vObrigatorio">Login</label>
                            <em class="formee-req">*</em>
                            <input type="text" name="loginPub" id="loginPub"/>

                            <label for="senhaPub" class="vObrigatorio">Senha</label>
                            <em class="formee-req">*</em>
                            <input type="password" name="senhaPub" id="senhaPub"/>

                        </div>
                        <div class="grid-12-12">
                            <input class="formee-button" type="submit" value="Entrar" />
                        </div>
                    </fieldset>
                </form>

            </div>
        </div>

    </body>
</html>